package com.cjfather.redis.steam.annotation.consumers;

import java.lang.reflect.Method;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.cjfather.redis.steam.annotation.AnnoRedisStreamListener;
import com.cjfather.redis.steam.annotation.constants.RedisConstants;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author: todd
 * @date:   2023-08-08
 */

@Component
@Slf4j
@Scope("prototype")
@Data
public class TestObjeStringStreamConsumer2  extends RedisTemplateResource implements AnnoObjeStringConsumerI {
	/**
     * 消费组
     */
    private String group;
    /**
     * 消费组中的某个消费者
     */
    private String consumerName;
    
	@Override
	@AnnoRedisStreamListener(streamKey = RedisConstants.STREAM_KEY3, consumerGroup = RedisConstants.STREAM_GROUP3, consumerName = RedisConstants.CONSUMER_NAME3,consumerSize = 2)
	public void onMessage(ObjectRecord<String, String> message) {
		try {
			Method method = TestObjeStringStreamConsumer2.class.getDeclaredMethod("onMessage", ObjectRecord.class);
			AnnoRedisStreamListener annotation = method.getAnnotation(AnnoRedisStreamListener.class);
			log.info("{} {} {}",annotation.streamKey(),annotation.consumerGroup(),annotation.consumerName());
		} catch (NoSuchMethodException | SecurityException e) {
			log.error("get key err:" + e.getMessage(),e);
		}
		
		String key = message.getStream();
		RecordId messageId = message.getId();
		String value = message.getValue();
		redisStreamCommonService.delRecord(key,messageId);;
		log.info("key:{},组:{},消费者{} + stream message。messageId={},  body={}",key, group,consumerName,messageId, value);
	}

}
